package com.csust.travel.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csust.travel.entity.ProductInfo;
import com.csust.travel.entity.ProductOrder;
import com.csust.travel.mapper.ProductOrderMapper;
import com.csust.travel.service.ProductInfoService;
import com.csust.travel.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;


@Service
public class ProductOrderServiceImpl extends ServiceImpl<ProductOrderMapper, ProductOrder> implements ProductOrderService {


    @Autowired
    private ProductInfoService productInfoService;

    @Override
    public HashMap<String, String> getUserOrderStat() {
        HashMap<String, String> result = new HashMap<>();
        String userId = StpUtil.getLoginIdAsString();
        //1.买入
        List<ProductOrder> buyList = lambdaQuery().eq(ProductOrder::getUserId, userId).list();
        result.put("buy", String.valueOf(buyList.size()));
        //2.卖出
        List<ProductOrder> sellList = lambdaQuery().eq(ProductOrder::getProductUserId, userId).list();
        result.put("sell", String.valueOf(sellList.size()));
        //3.发布
        List<ProductInfo> publishList = productInfoService.lambdaQuery().eq(ProductInfo::getUserId, userId).list();
        result.put("publish", String.valueOf(publishList.size()));
        List<ProductInfo> myProductCollectInfo = productInfoService.getMyProductCollectInfo();
        result.put("collect", String.valueOf(myProductCollectInfo.size()));
        return result;
    }

    @Override
    public List<ProductOrder> getMySellOrder(){
        String userId =StpUtil.getLoginIdAsString();
        List<ProductOrder> list = lambdaQuery().eq(ProductOrder::getProductUserId ,userId).orderByDesc(ProductOrder::getCreateTime).list();
        return list;
    }

    @Override
    public List<ProductOrder> getMyBuyOrder(){
        String userId = StpUtil.getLoginIdAsString();
        List<ProductOrder> list = lambdaQuery().eq(ProductOrder::getUserId,userId).orderByDesc(ProductOrder::getCreateTime).list();
        return list;
    }
}
